package com.efast.cafe.portal.controller.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.LanguageUtil;
import com.efast.cafe.portal.bean.base.BaseCompanyInfoBean;
import com.efast.cafe.portal.bean.company.PortalCompanyOrgBean;
import com.efast.cafe.portal.bean.company.PortalCompanyReporttoBean;
import com.efast.cafe.portal.bean.company.PortalCompanyUserBean;
import com.efast.cafe.portal.entity.company.PortalCompanyUserReporttoTreeDto;
import com.efast.cafe.portal.service.base.IBaseCompanyInfoService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserReporttoService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserService;

/******************************************************************************
 * @File name   :      PortalCompanyUserReporttoController.java
 *
 * @Author      :      xl
 *
 * @Date        :      2016-8-22
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-8-22 下午3:45:04        xl     1.0            Initial Version
 *****************************************************************************/
@Controller
@RequestMapping("/frame_center/company")
public class PortalCompanyUserReporttoController extends AbstractController{

	@Autowired
	private IPortalCompanyUserReporttoService portalCompanyUserReporttoService;
	@Autowired
	private IBaseCompanyInfoService baseCompanyInfoService;
	@Autowired
	private IPortalCompanyUserService portalCompanyUserService;
	
	

/**
 * 获取当前公司结构树
 * @Date        :      2016-8-26
 * @return
 */
	@RequestMapping(value = "/reportto/tree")
	public @ResponseBody ResultBean getCompanyTree(
			HttpSession session,
			@RequestBody Map map){
		ResultBean result=new ResultBean(true,"");
		BaseCompanyInfoBean companyInfo= baseCompanyInfoService.queryByCode();
		PortalCompanyUserReporttoTreeDto dto=new PortalCompanyUserReporttoTreeDto();
		dto.setId("org_"+0);
		dto.setParentId(null);
		dto.setName(LanguageUtil.getValueByJsonLocale(companyInfo.getCompany_name()));
		dto.setIconSkin("company");
		dto.setIsHidden(false);
		dto.setType("company");
		List<PortalCompanyUserReporttoTreeDto>  data=portalCompanyUserReporttoService.getCompanyTree();
		data.add(dto);
		List<PortalCompanyOrgBean> listResult = new ArrayList<>();
		for (int i = 0; i < data.size(); i++) {
			PortalCompanyOrgBean portalCompanyOrgBean1 = new PortalCompanyOrgBean();
			portalCompanyOrgBean1.setTitle(data.get(i).getName());
			portalCompanyOrgBean1.setData(data.get(i));
			listResult.add(portalCompanyOrgBean1);
		}
		result.setObject(listResult);
		return result;
	}
	
	
	/**
	 * 用户选择身份树
	 * @Date        :      2016-8-26
	 * @return
	 */
		@RequestMapping(value = "/identity/tree")
		public @ResponseBody ResultBean getCompanyIdentityTree(
				HttpSession session){
			ResultBean result=new ResultBean(true,"");
			BaseCompanyInfoBean companyInfo= baseCompanyInfoService.queryByCode();
			PortalCompanyUserReporttoTreeDto dto=new PortalCompanyUserReporttoTreeDto();
			dto.setId("org_"+0);
			dto.setParentId("company_"+LanguageUtil.getValueByJsonLocale(companyInfo.getCompany_name()));
			dto.setName(LanguageUtil.getValueByJsonLocale(companyInfo.getCompany_name()));
			dto.setIconSkin("company");
			dto.setIsHidden(false);
			dto.setType("company");
			List<PortalCompanyUserReporttoTreeDto>  data=portalCompanyUserReporttoService.getCompanyIdentityTree();
			data.add(dto);
			List<PortalCompanyOrgBean> listResult = new ArrayList<>();
			for (int i = 0; i < data.size(); i++) {
				PortalCompanyOrgBean portalCompanyOrgBean1 = new PortalCompanyOrgBean();
				portalCompanyOrgBean1.setTitle(data.get(i).getName());
				portalCompanyOrgBean1.setData(data.get(i));
				listResult.add(portalCompanyOrgBean1);
			}
			result.setObject(listResult);
			return result;
		}
		/**
		 * 用户org保存到session
		 * @Date        :      2016-8-26
		 * @return
		 */
	
		
		@RequestMapping(value = "/identity/session")
		public @ResponseBody ResultBean addSessionByOrg(
				HttpServletRequest request, HttpServletResponse response,
				@RequestBody PortalCompanyUserReporttoTreeDto bean){
			ResultBean result=new ResultBean(true,"");
			//用户信息存session中
			HttpSession session = request.getSession(false);
    		session.setAttribute(CommonConstant.LOGIN_USER_ORG_KEY, bean);
			return result;
		}
		
		@RequestMapping(value = "/identity/defaultPosition")
		public @ResponseBody ResultBean choiceDefaultPosition(
				HttpServletRequest request, HttpServletResponse response,
				@RequestBody PortalCompanyUserReporttoTreeDto bean){
			ResultBean result=new ResultBean(true,"");
			//保存或者查看用户默认职位
			result.setObject( portalCompanyUserReporttoService.choiceDefaultPosition(bean));
			
			return result;
		}
	/**
	 * 获取选中用户的审批人
	 * @Date        :      2016-8-26
	 * @param username
	 * @param positionId
	 * @param names
	 * @return
	 */
	@RequestMapping(value = "/reportto/user")
	public @ResponseBody ResultBean getUserMultiple(
			@RequestBody PortalCompanyReporttoBean bean){
		ResultBean result=new ResultBean(true,"");
		
		String[] names=bean.getNames().toArray(new String[bean.getNames().size()]);
		result.setObject(portalCompanyUserReporttoService.getUserMultiple(bean.getUsername(),names,bean.getPositionId(),bean.getOrgId()));
		return result;
	}
	
	/**
	 * 获取职位的 对应的用户
	 * @Date        :      2016-8-26
	 * @param names
	 * @return
	 */
	@RequestMapping(value = "/reportto/position")
	public @ResponseBody ResultBean getPositionMultiple(@RequestBody PortalCompanyReporttoBean bean){
		ResultBean result=new ResultBean(true,"");
		String[] names = bean.getNames().toArray(new String[bean.getNames().size()]);
		result.setObject(portalCompanyUserReporttoService.getPositionMultiple(names));
		return result;
	}
	
	/**
	 * 保存选中用户的审批人
	 * @Date        :      2016-8-26
	 * @param username
	 * @param positionId
	 * @param names
	 * @return
	 */
	@RequestMapping(value = "/reportto/reporttouser")
	public @ResponseBody ResultBean reporttoUser(@RequestBody PortalCompanyReporttoBean bean){
		ResultBean result=new ResultBean(true,"");
		try {
			 Long positionId=bean.getPositionId();
			 String username=bean.getUsername();
			if(bean.getUserIds()!=null&&bean.getUserIds().size()>0){
			     List<PortalCompanyUserBean> userList=portalCompanyUserService.queryUserInfoBy(bean.getUserIds(), 4);
			    List<String> usernames=new ArrayList<>();
			     for(int i=0;i<userList.size();i++){
			    	 usernames.add(userList.get(i).getUsername());
			     }
			     String[] names = usernames.toArray(new String[usernames.size()]);
			     portalCompanyUserReporttoService.reporttoUser(username,bean.getPositionId(),bean.getOrgId(),names);
			}
			else{
				 String[] names=new String [0];
				 portalCompanyUserReporttoService.reporttoUser(username,bean.getPositionId(),bean.getOrgId(),names);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		
		return result;
	}
	
	/**
	 * 保存职位 对应的用户
	 * @Date        :      2016-8-26
	 * @param positionId
	 * @param names
	 * @return
	 */
	@RequestMapping(value = "/reportto/reporttoposition")
	public @ResponseBody ResultBean reporttoPosition(
		@RequestBody PortalCompanyReporttoBean bean){
		ResultBean result=new ResultBean(true,"");
		try {
			// Long positionId=Long.parseLong(bean.getPositionId().toString());
			if(bean.getUserIds()!=null&&bean.getUserIds().size()>0){
			     List<PortalCompanyUserBean> userList=portalCompanyUserService.queryUserInfoBy(bean.getUserIds(), 4);
			    List<String> usernames=new ArrayList<>();
			     for(int i=0;i<userList.size();i++){
			    	 usernames.add(userList.get(i).getUsername());
			     }
			     String[] names = usernames.toArray(new String[usernames.size()]);
			    
			portalCompanyUserReporttoService.reporttoPosition(bean.getPositionId(),bean.getOrgId(),names);
			}else{
				 String[] names=new String [0];
				portalCompanyUserReporttoService.reporttoPosition(bean.getPositionId(),bean.getOrgId(),names);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}


	/**
	 * 保存职位对应的用户
	 * @Date        :      2016-8-26
	 * @return
	 */
	@RequestMapping(value = "/user/addUserByOrg")
	public @ResponseBody ResultBean addUserByOrg(
			@RequestBody PortalCompanyReporttoBean bean){
		ResultBean result=new ResultBean(true,"");
		try {
			// Long positionId=Long.parseLong(bean.getPositionId().toString());
			if(bean.getUserIds()!=null&&bean.getUserIds().size()>0){
				List<PortalCompanyUserBean> userList=portalCompanyUserService.queryUserInfoBy(bean.getUserIds(), 4);
				List<String> usernames=new ArrayList<>();
				for(int i=0;i<userList.size();i++){
					usernames.add(userList.get(i).getUsername());
				}
				portalCompanyUserReporttoService.addUserByOrg(bean.getPositionId(),bean.getOrgId(),usernames);
			}else{
				result.setSuccess(false);
				result.setErrorCode("请选择用户");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}


	/**
	 * 删除职位对应的用户
	 * @Date        :      2016-8-26
	 * @return
	 */
	@RequestMapping(value = "/user/removeUserByOrg")
	public @ResponseBody ResultBean removeUserByOrg(
			@RequestBody PortalCompanyReporttoBean bean){
		ResultBean result=new ResultBean(true,"");
		try {
			// Long positionId=Long.parseLong(bean.getPositionId().toString());
			if(bean.getUserIds()!=null&&bean.getUserIds().size()>0){
				List<PortalCompanyUserBean> userList=portalCompanyUserService.queryUserInfoBy(bean.getUserIds(), 4);
				List<String> usernames=new ArrayList<>();
				for(int i=0;i<userList.size();i++){
					usernames.add(userList.get(i).getUsername());
				}
				portalCompanyUserReporttoService.deleteUserByOrg(bean.getPositionId(),bean.getOrgId(),usernames);
			}else{
				result.setSuccess(false);
				result.setErrorCode("请选择用户");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}
	
}
